 <?php
//require "dbcon.php";
include "simplehtmldom/simple_html_dom.php";

function Dantri_TrangChu($url) {
    $linkarray=array();
    $html = file_get_html($url);
    foreach ($html->find(".fon1 mt2") as $link){            
        if ($link->href==NULL)  continue;
        if ($link->plaintext==NULL) continue;
        $text=str_replace("&nbsp;"," ",$link->plaintext);
        $text=trim($text);        
        if ($text=="") continue;
        if (substr($link->href,0,1)=="/") $link->href=$url. $link->href;
        if (in_array($link->href,$linkarray)==false) $linkarray[$text]=$link->href;
    }
    foreach ($html->find(".fon6") as $link){            
        if ($link->href==NULL)  continue;
        if ($link->plaintext==NULL) continue;
        $text=str_replace("&nbsp;"," ",$link->plaintext);
        $text=trim($text);        
        if ($text=="") continue;
        if (substr($link->href,0,1)=="/") $link->href=$url. $link->href;
        if (in_array($link->href,$linkarray)==false) $linkarray[$text]=$link->href;
    }
    
    
        foreach ($html->find(".ul1 li a") as $link){            
        if ($link->href==NULL)  continue;
        if ($link->plaintext==NULL) continue;
        $text=str_replace("&nbsp;"," ",$link->plaintext);
        $text=trim($text);        
        if ($text=="") continue;
        if (substr($link->href,0,1)=="/") $link->href=$url. $link->href;
        if (in_array($link->href,$linkarray)==false) $linkarray[$text]=$link->href;
    }
        foreach ($html->find(".fon4") as $link){            
        if ($link->href==NULL)  continue;
        if ($link->plaintext==NULL) continue;
        $text=str_replace("&nbsp;"," ",$link->plaintext);
        $text=trim($text);        
        if ($text=="") continue;
        if (substr($link->href,0,1)=="/") $link->href=$url. $link->href;
        if (in_array($link->href,$linkarray)==false) $linkarray[$text]=$link->href;
    }
    $html->clear();
    unset($html);
    return $linkarray;
}
// funtion getContent($array, $keyword)
// {
	// $td = $html->find($keyword,0);
	// if($td=="") return '';
	// else return td->innertext;
// }
function Dantri_Lay1Tin($urlwebsite,$url) {
    $html = file_get_html($url);
	//remove links to dantri
	// foreach($html->find('a') as $e) 
	// {
		// $e->href = '';
	// }
    $tin = array();
	//get title from website
    $td = $html->find('.fon31',0);
	if($td=="") $tin['tieude'] = '';
    else
	{	
		$tin['tieude']=$td->innertext;
		
		$td->outertext='';
	}
	
	//get summary from website
    $tt = $html->find('.fon33',0);
	if($tt=="") $tin['tomtat']='';
    else 
	{
		//remove href
		//$tin['tomtat']=strip_tags($tt->innertext);
		$tin['tomtat']='';
		$tt->outertext = '';
	}
	
	//get content from website
    $content = $html->find('.fon34',0);    
	if($content=="")$tin['content']='';
	else{    
		foreach( $content->find('img') as $img) {        
			if (substr($img->src,0,1) == "/") 
			$img->src = $urlwebsite.$img->src;
		}
		$tin['content'] = $content->innertext;
	}
	
    $html->clear();
    unset($html);
    return $tin;
}
?> 